import java.util.Vector;


public class Player extends GameObject
{   
    static int INIT_MAX_HP = 50;
    static int INIT_MAX_MP = 50;
    static int MANA_REGEN = 5; //per turn
    
    int currentHP;
    int maxHP;
    int currentMP;
    int maxMP;
    double attackRating = 1.0;
    double defenseRating = 1.0;
    Spell activeSpell;
    Vector<Spell> spells; // available spells
    Vector<Loot> equipment;
    Loot wand;
    
    int firePower = 5;
    int waterPower = 5;
    int earthPower = 5;
    int lightningPower = 5;
    
    void attack(Direction d)
    {
        if(this.wand == null)
        {
            return;
        }
        if (activeSpell.element == Element.Fire)
            this.currentMP = firePower*activeSpell.manaCost;
        if (activeSpell.element == Element.Water)
            this.currentMP = waterPower*activeSpell.manaCost;
        if (activeSpell.element == Element.Earth)
            this.currentMP = earthPower*activeSpell.manaCost;
        if (activeSpell.element == Element.Lightning)
            this.currentMP = lightningPower*activeSpell.manaCost;
        if(this.currentMP >= 1)
        {
            GameObject.gameObjects.add(
                SpellCreator.copy(
                    wand.damageModifier, activeSpell,true, this.x, this.y, d));
            if (activeSpell.element == Element.Fire)
            {
                firePower--;
                int orbs = (int)Math.ceil((double)firePower / 5);
                mainFrame.updateElementCount(2, -(mainFrame.fireIcon-orbs));
                mainFrame.changeOrbs(2);
            }
            if (activeSpell.element == Element.Water)
            {
                waterPower--;
                int orbs = (int)Math.ceil((double)waterPower / 5);
                mainFrame.updateElementCount(1, -(mainFrame.waterIcon-orbs));
                mainFrame.changeOrbs(1);
            }
            if (activeSpell.element == Element.Earth)
            {
                earthPower--;
                int orbs = (int)Math.ceil((double)earthPower / 5);
                mainFrame.updateElementCount(3, -(mainFrame.earthIcon-orbs));
                mainFrame.changeOrbs(3);
            }
            if (activeSpell.element == Element.Lightning)
            {
                lightningPower--;
                int orbs = (int)Math.ceil((double)lightningPower / 5);
                mainFrame.updateElementCount(4, -(mainFrame.lngIcon-orbs));
                mainFrame.changeOrbs(4);
            }
                
        }
    }
    
    void setNextMove(Direction d)
    {
        int nextX = this.x;
        int nextY = this.y;

        if(d == Direction.Down)
        {
            nextY++;
        }
        if(d == Direction.Up)
        {
            nextY--;
        }
        if(d == Direction.Left)
        {
            nextX--;
        }
        if(d == Direction.Right)
        {
            nextX++;
        }
    }
    
    void setOrientation(Direction d) 
    {
        if(d == Direction.Down)
        {
            this.filePath = "src/images/Toot/tootFinal.PNG";
        }
        if(d == Direction.Up)
        {
            this.filePath = "src/images/Toot/tootFinal.PNG";
        }
        if(d == Direction.Left)
        {
            this.filePath = "src/images/Toot/tootFinal.PNG";
        }
        if(d == Direction.Right)
        {
            this.filePath = "src/images/Toot/tootFinal.PNG";
        }
    }

    public void move(Direction d)
    {
        setOrientation(d);
        //setNextMove(d);
        int nextX = this.x;
        int nextY = this.y;

        if(d == Direction.Down){nextY++;}
        else if(d == Direction.Up){nextY--;}
        else if(d == Direction.Left){nextX--;}
        else if(d == Direction.Right){nextX++;}
                
        for(GameObject go: GameObject.gameObjects)
        {
            if(nextX == go.x && nextY == go.y)
            {
                if(go instanceof Loot)
                {
                    addLoot((Loot)go);
                }
                else if(go instanceof Spell)
                {
                    //allow movement to square, take damage if enemy spell
                    if(!((Spell)go).playerSpell)
                    {
                        int baseDamage = ((Spell)go).damage;
                        double defense = this.defenseRating;
                        if(this.defenseRating <=0.5)
                        {
                            defense = 0.5;
                        }
                        this.currentHP-=baseDamage/defense;
                        GameObject.gameObjects.remove(go);
                    }
                }
                else if(go instanceof Exit)
                {
                    System.out.println("sadsadsadsad");
                    nextX = this.x;
                    nextY = this.y;
                    if (((Exit)go).map != null)
                    {
                        Exit e = ((Exit)go); 
                        System.out.println("sa121212d");
                        GameObject.player.x = e.playerStartX;
                        GameObject.player.y = e.playerStartY;
                        GameObject.loadNewMap(((Exit)go).map);
                    }
                }
                else
                {
                    nextX = this.x;
                    nextY = this.y;
                }
            }       
        }
        if(this.x != nextX || this.y!=nextY)
        {
                if(this.x <13 && this.y<13)
                {
                    map[this.x][this.y].gameObject = null;
                    map[this.x][this.y].update();
                }
                if(nextX <13 && nextY<13)
                {
                    map[nextX][nextY].gameObject = this;
                    map[nextX][nextY].update();
                }
        }
        this.x = nextX;
        this.y = nextY;     
    }
    
    void addLoot(Loot loot)
    {
        loot.x = -5;
        loot.y = -5;
        if(loot.type == LootEnum.Orb)
        {
            useLoot(loot);
        }
        else
        {
            this.equipment.add(loot);
        }
    }
    
    void useLoot(Loot loot)
    {
        if(loot.type == LootEnum.Orb)
        {
            if(loot.element == Element.Fire)
            {
                this.firePower+=5;
                mainFrame.addElement(2);
            }
                
            if(loot.element == Element.Water)
            {
                this.waterPower+=5;
                mainFrame.addElement(1);
            }
                
            if(loot.element == Element.Lightning)
            {
                this.lightningPower+=5;
                mainFrame.addElement(4);
            }
                
            if(loot.element == Element.Earth)
            {
                this.earthPower+=5;
                mainFrame.addElement(3);
            }
                            
        }
        if(loot.type == LootEnum.Wand)
        {
            wand = loot;
        }
        this.currentHP+=loot.gainHP;
        this.currentMP+=loot.gainMP;
        this.maxHP+=loot.maxHPbonus;
        this.maxMP+=loot.maxMPbonus;
        this.attackRating += loot.damageModifier;
        this.defenseRating += loot.defenseModifer;
        if(loot.isOneTimeUse)
        {
            loot.x = 20;
            loot.y = 20;
            //this.equipment.remove(loot);
        }
    }
    

    void nextAttack(int i)
    {
        activeSpell = spells.get(i);
    }
    
    void takeDamage(double baseDamage)
    {
        double defense = this.defenseRating;
        if(this.defenseRating <=0.5)
        {
            defense = 0.5;
        }
        double percent = (this.currentHP - baseDamage/defense) / this.currentHP;
        mainFrame.setHP(percent);
        this.currentHP-=baseDamage/defense;
    }
    
    public Player(int x, int y)
    {
        this.equipment = new Vector<Loot>();
        this.spells = new Vector<Spell>();
        this.activeSpell = SpellCreator.water1(0.1,true, -1, -1, Direction.Up);
        this.spells.add(this.activeSpell);
        this.spells.add(SpellCreator.fire1(0.1,true, -1, -1, Direction.Up));
        this.spells.add(SpellCreator.wind1(0.1,true, -1, -1, Direction.Up));
        this.spells.add(SpellCreator.lightning1(0.1,true, -1, -1, Direction.Up));
        this.equipment.add(LootCreator.BrokenWand(-1, -1));
        this.x = x;
        this.y = y;
        this.filePath="src/images/Toot/tootFinal.PNG";
        this.wand = LootCreator.BrokenWand(-2, -2);
        this.maxHP = INIT_MAX_HP;
        this.maxMP = INIT_MAX_MP;
        this.currentHP = this.maxHP;
        this.currentMP = this.maxMP;
    }
    
}
